$(function () {
    var markid = getUrlString('markid');
    var kuid = getUrlString('kuid');
    var isIphoneX = getUrlString('isIphoneX');
    var isLockon = getUrlString('isLockon');
    var isBuyBack = getUrlString('isBuyBack'),type = true,topHeight,exgprice,cost;
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    function detail() {
        if (isIphoneX === 'true'){
            $('.TC').show();
            $('.goXianYu').css('bottom','1.5rem');
            $('.commodityInfo').css('padding-bottom','1.5rem')
        }
        if(isLockon === '1'){
            $('.goXianYu .provateChat,.goXianYu .like').hide();
            $('.goXianYu .unlock').show();
        }else {
            $('.goXianYu .unlock').hide();
            $('.goXianYu .provateChat,.goXianYu .like').show();
        }
        var data = {
            'markid':markid,
            'kuid':kuid
        };
        getData('post','/niu/apiv3/niu_getkuinfo.php',data,function (data) {
            console.log(data);
            exgprice = Number(data.toys.exgprice);
            cost = Number(data.toys.cost);
            $('.toyspic').attr('src',data.toys.pics);
            $('.toysname').html(data.toys.name);
            $('.outtime').html(data.outtime.substring(4,6) + '月' + data.outtime.substring(6,8) + '日');
            var time = data.toys.book_time.substring(0,4) + '年' + data.toys.book_time.substring(4,6)+'月';
            if (data.toys.book_time === '0'){
                $('.fssj').html('无');
            } else {
                $('.fssj').html(time);
            }
            if (data.toys.sptype === '2'){
                $('.spzt').html('预售商品');
            } else {
                $('.spzt').html('现货商品')
            }
            $('.commodityPicture').html(escape2Html(data.toys.intro));
            if (data.backprice === 0 && data.backprice2 === 0){
                type = false;
                $('.choumoney2').html('不可回购');
            }else if(data.backprice === 0){
                type = true;
                $('.huigoujin').hide().removeClass('active');
                $('.huigouyin').show().addClass('active');
                $('.choumoney2').html(data.backprice2 + '银币');
                $('.hgData').html(data.backprice2)
            }else {
                type = true;
                $('.huigouyin').hide();
                $('.huigoujin').show();
                $('.choumoney2').html(data.backprice + 'HP/金币');
                $('.hgData').html(data.backprice)
            }

            if(isBuyBack === '1'){
                if (type){
                    $('#conhgAlert2').show();
                }else {
                    laymsg('该商品不可回购')
                }
            }else {
                $('#conhgAlert2').hide();
            }

            $('#price').html(data.toys.price);
            $('#mainseries').html(data.toys.mainseries);
            $('#maker').html(data.toys.maker);
            $('#secondseries').html(data.toys.secondseries);
        })
    }
    detail();

    //解锁
    $('.unlock').click(function () {
        var data = {
            'markid':markid,
            'set':'unlock',
            'kutoysids':kuid
        };
        getData('post','/niu/apiv3/niu_dolockon.php',data,function (data) {
            console.log(data);
            if(data.resultcode === "0" ){
                laymsg('解锁成功');
                setTimeout(function () {
                    wx.miniProgram.navigateBack({
                        delta:1
                    });
                },500);
            }else {
                laymsg(data.msg)
            }
        })
    });
    //回购
    $("#ghconBtn2").click(function(){
        dohuigou()
    });

    $('.like').click(function () {
        if (type){
            $('#conhgAlert2').show();
        }else {
            laymsg('该商品不可回购')
        }
    });

    $('#ghcancel2').click(function () {
        $('#conhgAlert2').hide();
    });

    $('#conhgAlert2 .checktype2 p').click(function () {
        $('#conhgAlert2 .checktype2 p').removeClass('active');
        $(this).addClass('active');
        // let check =
        // checktoys(hgId,'show',check);
    });

    // 回购
    function dohuigou () {
        let isgold = $('#conhgAlert2 .checktype2 .active').find('input').val();
        let data = {
            'markid':markid,
            'set':'1',
            'kutoysid':kuid,
            'isgold':isgold
        };
        getData('post','/niu/apiv4/niu_new_dohuidian.php',data,function (data) {
            console.log(data);
            if(data.resultcode === "0" ){
                setTimeout(function () {
                    wx.miniProgram.navigateBack({
                        delta:1
                    });
                },500);
            }else {
                laymsg(data.msg)
            }
        })
    }
    //瓜咸蛋
    $('.provateChat').click(function () {
        $(".guaXD").show().removeClass('bounceOutDown').addClass('bounceInUp');
        // $('.guaXD').animate()
    });
    //取消
    $('.qx').click(function () {
        $(".guaXD").removeClass('bounceInUp').addClass('bounceOutDown');
    });
    let showrmb = 0,moneyrmb,show = 0;
    //同意协议
    $('.notyxdxy').click(function () {
        $(this).hide();
        $('.tyxdxy').show();
        show = 0;
    });
    //不同意协议
    $('.tyxdxy').click(function () {
        show = 1;
        $(this).hide();
        $('.notyxdxy').css('display','inline-block')
    });
    //同意挂卖到小程序
    $('.guaSmell .isNoty').click(function () {
        $(this).hide();
        $('.tyWXS').show();
        $('.guaSmell').attr('data-showRmb','1');
        // showrmb = 1;
    });
    //不同意挂卖到小程序
    $('.guaSmell .isTy').click(function () {
        // showrmb = 0;
        $(this).hide();
        $('.notyWXS').css('display','inline-block');
        $('.guaSmell').attr('data-showRmb','0');
    });
    //不同意挂卖app
    $('.guaApp .tyApp').click(function () {
        $(this).hide();
        // showApp = 0;
        $('.notyApp').css('display','inline-block');
        $('.guaApp').attr('data-showApp','0');
    });
    //同意挂卖app
    $('.guaApp .notyApp').click(function () {
        $(this).hide();
        $('.tyApp').show();
        $('.guaApp').attr('data-showApp','1');
        // showApp = 1;
    });
    //计算人民币
    $("#priceGold").bind("input propertychange", function() {
        let money = $(this).val();
        moneyrmb = money;
        let moneyRMB = Math.ceil(money * 6);
        $('#showrmbNum').html(moneyRMB);
        // console.log(moneyRMB);
    });
    //交易密码说明
    $('#passwprdInfo').click(function () {
        $('#conAlert2 .topInfo').html('打开密码交易将会生成随机密码，买家必须输入密码才可以购买商品。卖家可在咸蛋编辑页面刷新或关闭此项功能。注：该商品加密后将不在默认咸蛋页面显示，仅在筛选或搜索后显示。');
        $('#conAlert2').show();
    });
    //小程序说明
    $('#InfoDetail').click(function () {
        $('#conAlert2 .topInfo').html('同步以现金价格发布在蛋酱全有小程序内，在小程序内可以直接购买及提取收益。</br>' +
            '注1：现金交易后，蛋酱将收取交易总额的2%作为技术服务费，用户实际收到交易金额为扣除技术服务费后的成交金额。</br>' +
            '注2：商品售出金额将于成交24小时后才可以在小程序“咸蛋收益”进行提现。');
        $('#conAlert2').show();
    });

    $('#conBtn2').click(function () {
        $('#conAlert2').hide();
    });
    let isshow = 2;
    $(document).on('click','#gm', function () {
        if (show === 1){
            laymsg('请同意咸蛋交易协议');
            return
        }
        var money2 = $('#priceGold').val() * 6;
        let showRmb = $('.guaSmell').attr('data-showRmb');
        let showApp = $('.guaApp').attr('data-showApp');
        if (showRmb === '0' && showApp === '0'){
            showrmb = '';
            laymsg('请选择挂卖方式');
            return;
        }else if (showRmb === '1' && showApp === '1'){
            showrmb = 1;
            isshow = 2
        }else if (showRmb === '1'){
            showrmb = 1;
            isshow = 1
        }else if (showApp === '1'){
            showrmb = 0;
            isshow = 0
        }
        let moneyFW = Math.ceil((moneyrmb / 100 * 2 ));
        $('#moneyJin').html(money2+'金币');
        $('#moneyRMB').html('￥'+ moneyrmb);
        $('#moneyFW').html('￥'+ moneyFW);
        if (isshow === 0){
            $('.moneyRMB,.moneyFW').hide();
            $('.moneyJin').show();
        } else if(isshow === 1){
            $('.moneyJin').hide();
            $('.moneyRMB,.moneyFW').show();
        }else {
            $('.moneyJin,.moneyRMB,.moneyFW').show();
        }
        if(money2 === 0){
            laymsg('请输入挂卖价格');
            return;
        }

        if(money2 < 12){
            laymsg('挂卖价格不能小于12');
            return;
        }

        let price = exgprice*0.3;
        let costPrice = cost*0.3;
        if (price === 0){
            if (costPrice > money2 ){
                laymsg('挂卖价格风险较高，请重新定价或联系客服处理');
            }else {
                $('#conAlert').show();
            }
        }else {
            if (price > money2 ){
                laymsg('挂卖价格风险较高，请重新定价或联系客服处理');
            }else {
                $('#conAlert').show();
            }
        }
    });

    $('#conBtn').click(function () {
        upData()
    });
    $('#cancel2').click(function () {
        $('#conAlert').hide();
    });

    function upData () {
        let intro = $('#intro').val();
        let money2 = $('#priceGold').val()*6;
        let moneyrmbFen = moneyrmb * 100;
        let data = {
            'markid':markid,
            'kutoysid': kuid,
            'money2':money2,
            'intro':'',
            'showrmb':showrmb,
            'isshow':isshow,
            'moneyrmb':moneyrmbFen,
            'ispw':0,

        };

        getData('post','/niu/apiv3/xiandanv3_doto.php?method=addupdate',data,function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                laymsg(data.msg);
                $('#conAlert').hide();
                setTimeout(function () {
                    wx.miniProgram.navigateBack({
                        delta:1
                    });
                },500)
            }else {
                $('#conAlert').hide();
                laymsg(data.msg)
            }
        })
    }
});
